// Copyright 2007 the original author or authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package lichen.components.fckeditor;

import org.apache.tapestry5.Asset;
import org.apache.tapestry5.MarkupWriter;
import org.apache.tapestry5.RenderSupport;
import org.apache.tapestry5.annotations.Environmental;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.annotations.Path;
import org.apache.tapestry5.corelib.base.AbstractTextField;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.ioc.services.SymbolSource;
import org.apache.tapestry5.services.ClasspathAssetAliasManager;

/**
 * @author <a href=mailto:Ghostbb@bjmaxinfo.com>Ghostbb</a>
 * @version $Revision$
 * @since 0.0.3
 */
public class FckEditor extends AbstractTextField {
	
	/**
	 * The height of the editor
	 */
    @Parameter(defaultPrefix="literal", value="300px")
    private String height;

    /**
     * The width of the editor
     */
    @Parameter(defaultPrefix="literal", value="100%")
    private String width;
    
    /**
     * The toolbar set to be used with this editor.
     * Toolbar sets can be configured in a {@link #customConfiguration custom configuration}. 
     */
    @Parameter(defaultPrefix="literal")
    private String toolbarSet;
    
    /**
     * A custom configuration for this editor, see the fckeditor manual for details on custom configurations.
     */
    @Parameter
    private Asset customConfiguration;
    
    @Inject
    private ClasspathAssetAliasManager cpam;
    
    @Inject
    private SymbolSource symbolSource;
    
    public String getScript(String value) {
       	value = value.replace('\r', '\\'); //this is needed for javascript to be able to extend a string across multiple lines
    	value = value.replace("'", "&#39;"); //this is needed because the string delimiter is ' for the script below
    	
    	return
		String.format("var oFCKeditor = new FCKeditor( '%s' );\n", getClientId()) +
		String.format("oFCKeditor.BasePath = '%s';\n", cpam.toClientURL(symbolSource.expandSymbols("${lichen.components.fckeditorscript.path}"))+"/") + //todo could this be done in another way?
		(customConfiguration == null ? "" : String.format("oFCKeditor.Config[\"CustomConfigurationsPath\"] = '%s';\n", customConfiguration)) +
		(toolbarSet == null ? "" : String.format("oFCKeditor.ToolbarSet = '%s'\n", toolbarSet)) +
		String.format("oFCKeditor.Height = '%s';\n", height) +
		String.format("oFCKeditor.Width = '%s';\n", width) +
		String.format("oFCKeditor.Value = \'%s\';\n", value) +
		"oFCKeditor.Create() ;\n";
    }
    
    @Environmental
    private RenderSupport _pageRenderSupport;
	
    @Inject
    @Path("${lichen.components.fckeditorscript}/fckeditor.js")
    private Asset fckeditor;
    
    void beginRender(MarkupWriter writer) {
		_pageRenderSupport.addScriptLink(fckeditor);
	}

    @Override
    protected final void writeFieldTag(MarkupWriter writer, String value)
    {
        writer.element(
                "script",
                "type",
                "text/javascript");
        writer.writeRaw("<!--\n" + getScript(value) + "//-->\n");
    }

    final void afterRender(MarkupWriter writer)
    {
        writer.end(); // script
    }
}
